package cas.ihep.util;

import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.concurrent.atomic.AtomicInteger;

public class RandomGenerator {

    private AtomicInteger atomic=new AtomicInteger(0);
    private String pidname;
    private int pidindex;
    private RandomGenerator(){
        RuntimeMXBean runtime= ManagementFactory.getRuntimeMXBean();
        pidname=runtime.getName();
        pidindex=pidname.indexOf('@');
    }

    public static final RandomGenerator instance=new RandomGenerator();

    public Appendable next(Appendable app,Class<?> prefixCls,char seperator){
        try {
            app.append(prefixCls.getSimpleName());
            app.append(seperator);
            app.append(Long.toHexString(System.currentTimeMillis()));
            app.append(seperator);
            app.append(pidname,0,pidindex);
            app.append(seperator);
            app.append(Integer.toString(atomic.getAndIncrement()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return app;
    }

    public String next(Class<?> cls){
        return next(new StringBuilder(32),cls,'-').toString();
    }
}
